package org.rsbot.script.wrappers;

import java.awt.Polygon;
import java.awt.Rectangle;
import java.util.ArrayList;

public class RSArea
{
  private Polygon area;
  private int plane;

  public RSArea(RSTile[] paramArrayOfRSTile, int paramInt)
  {
    this.area = tileArrayToPolygon(paramArrayOfRSTile);
    this.plane = paramInt;
  }

  public RSArea(RSTile[] paramArrayOfRSTile)
  {
    this(paramArrayOfRSTile, 0);
  }

  public RSArea(RSTile paramRSTile1, RSTile paramRSTile2, int paramInt)
  {
    this(new RSTile[] { paramRSTile1, new RSTile(paramRSTile2.getX() + 1, paramRSTile1.getY()), new RSTile(paramRSTile2.getX() + 1, paramRSTile2.getY() + 1), new RSTile(paramRSTile1.getX(), paramRSTile2.getY() + 1) }, paramInt);
  }

  public RSArea(RSTile paramRSTile1, RSTile paramRSTile2)
  {
    this(paramRSTile1, paramRSTile2, 0);
  }

  public RSArea(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    this(new RSTile(paramInt1, paramInt2), new RSTile(paramInt3, paramInt4), 0);
  }

  public boolean contains(int paramInt1, int paramInt2)
  {
    return contains(new RSTile[] { new RSTile(paramInt1, paramInt2) });
  }

  public boolean contains(int paramInt, RSTile[] paramArrayOfRSTile)
  {
    return (this.plane == paramInt) && (contains(paramArrayOfRSTile));
  }

  public boolean contains(RSTile[] paramArrayOfRSTile)
  {
    RSTile[] arrayOfRSTile1 = getTileArray();
    for (RSTile localRSTile1 : paramArrayOfRSTile) {
      for (RSTile localRSTile2 : arrayOfRSTile1) {
        if (localRSTile1.equals(localRSTile2)) {
          return true;
        }
      }
    }
    return false;
  }

  public RSTile getCentralTile()
  {
    if (this.area.npoints < 1)
      return null;
    int i = 0; int j = 0;
    for (int k = 0; k < this.area.npoints; k++) {
      i += this.area.xpoints[k];
      j += this.area.ypoints[k];
    }
    return new RSTile(Math.round(i / this.area.npoints), Math.round(j / this.area.npoints));
  }

  public RSTile getNearestTile(RSTile paramRSTile)
  {
    RSTile[] arrayOfRSTile1 = getTileArray();
    Object localObject = null;
    double d1 = -1.0D;
    for (RSTile localRSTile : arrayOfRSTile1) {
      double d2 = distanceBetween(localRSTile, paramRSTile);
      if (localObject == null) {
        d1 = d2;
        localObject = localRSTile;
      } else if (d2 < d1) {
        localObject = localRSTile;
        d1 = d2;
      }
    }
    return localObject;
  }

  public RSTile[] getTileArray()
  {
    ArrayList localArrayList = new ArrayList();
    for (int i = getX(); i <= getX() + getWidth(); i++) {
      for (j = getY(); j <= getY() + getHeight(); j++) {
        if (this.area.contains(i, j)) {
          localArrayList.add(new RSTile(i, j));
        }
      }
    }
    RSTile[] arrayOfRSTile = new RSTile[localArrayList.size()];
    for (int j = 0; j < localArrayList.size(); j++)
      arrayOfRSTile[j] = ((RSTile)localArrayList.get(j));
    return arrayOfRSTile;
  }

  public RSTile[][] getTiles()
  {
    RSTile[][] arrayOfRSTile = new RSTile[getWidth() + 1][getHeight() + 1];
    for (int i = 0; i < getWidth(); i++) {
      for (int j = 0; j < getHeight(); j++) {
        if (this.area.contains(getX() + i, getY() + j)) {
          arrayOfRSTile[i][j] = new RSTile(getX() + i, getY() + j);
        }
      }
    }
    return arrayOfRSTile;
  }

  public int getWidth()
  {
    return this.area.getBounds().width;
  }

  public int getHeight()
  {
    return this.area.getBounds().height;
  }

  public int getX()
  {
    return this.area.getBounds().x;
  }

  public int getY()
  {
    return this.area.getBounds().y;
  }

  public int getPlane()
  {
    return this.plane;
  }

  public Rectangle getBounds()
  {
    return new Rectangle(this.area.getBounds().x + 1, this.area.getBounds().y + 1, getWidth(), getHeight());
  }

  private Polygon tileArrayToPolygon(RSTile[] paramArrayOfRSTile)
  {
    Polygon localPolygon = new Polygon();
    for (RSTile localRSTile : paramArrayOfRSTile) {
      localPolygon.addPoint(localRSTile.getX(), localRSTile.getY());
    }
    return localPolygon;
  }

  private double distanceBetween(RSTile paramRSTile1, RSTile paramRSTile2)
  {
    return Math.sqrt((paramRSTile1.getX() - paramRSTile2.getX()) * (paramRSTile1.getX() - paramRSTile2.getX()) + (paramRSTile1.getY() - paramRSTile2.getY()) * (paramRSTile1.getY() - paramRSTile2.getY()));
  }
}